package com.handinfo.android.game.skill;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.object.Scene;
import com.handinfo.android.game.GameActor;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.utils.Tools;

public class SkillResult
{
	public SkillResult()
	{

	}

	public SkillResult(int type, long id, int value)
	{
		m_tgtType = type;
		m_tgtId = id;
		m_tgtValue = value;
	}

	public int m_tgtType;
	public long m_tgtId;
	public int m_tgtValue; //数值
	public int m_tgtResult; //1：命中   2：暴击
	public int m_tgtCurHp; //目标当前血量
	public Bitmap m_img_extra; //1：经验 2：荣誉
	public Bitmap m_img_num;

	public int m_duration;
	public int m_x, m_y;

	public static Bitmap s_damageNum_yellow, s_damageNum_green, s_damageNum_red;
	
	public static byte[] IMG_DAMAGE_NUM_GREEN_SIZE = { 24, 36 };
	public static byte[] IMG_DAMAGE_NUM_RED_SIZE = { 24, 36 };

	public static final String IMG_DAMAGE_NUM_YELLOW_URL = "/img/newui/shuzhi_1.gnp";
	public static final String IMG_DAMAGE_NUM_RED_URL = "/img/newui/shuzhi_2.gnp";
	public static final String IMG_DAMAGE_NUM_GREEN_URL = "/img/newui/shuzhi_3.gnp";

	public static Bitmap s_img_skillResult_miss, s_img_skillResult_absorb, s_img_skillResult_noeffect;
	public static final String IMG_SKILLRESULT_MISS_URL = "/img/newui/shanbi.gnp";
	public static final String IMG_SKILLRESULT_ABSORB_URL = "/img/newui/xishou.gnp";
	public static final String IMG_SKILLRESULT_NOEFFECT_URL = "/img/newui/chuantou_1.gnp";//"/img/newui/wuxiao.gnp";

	public static Bitmap s_img_extra_exp, s_img_extra_rongyu, s_img_extra_zhangong, s_img_extra_jinghun,
			s_img_extra_xiuwei;
	public static final String IMG_EXTRA_EXP_URL = "/img/newui/renwujingyan.gnp";
	public static final String IMG_EXTRA_RONGYU_URL = "/img/newui/renwurongyu.gnp";
	public static final String IMG_EXTRA_ZHANGONG_URL = "/img/newui/renwuzhangong.gnp";
	public static final String IMG_EXTRA_JINGHUN_URL = "/img/newui/renwujinghun.gnp";
	public static final String IMG_EXTRA_XIUWEI_URL = "/img/newui/renwuxiuwei.gnp";

	public static void initStaticResource()
	{
		if (DWGameManager.AdpaterType == DWGameManager.AD_HIGH) {
			IMG_DAMAGE_NUM_GREEN_SIZE = new byte[] { 35, 47 };
			IMG_DAMAGE_NUM_RED_SIZE = new byte[] { 35, 47 };
		}
		else {
			IMG_DAMAGE_NUM_GREEN_SIZE = new byte[] { 24, 36 };
			IMG_DAMAGE_NUM_RED_SIZE = new byte[] { 24, 36 };
		}
		s_damageNum_yellow = UIWindows.createImage(IMG_DAMAGE_NUM_YELLOW_URL);
		s_damageNum_green = UIWindows.createImage(IMG_DAMAGE_NUM_GREEN_URL);
		s_damageNum_red = UIWindows.createImage(IMG_DAMAGE_NUM_RED_URL);
		s_img_skillResult_miss = UIWindows.createImage(IMG_SKILLRESULT_MISS_URL);
		s_img_skillResult_absorb = UIWindows.createImage(IMG_SKILLRESULT_ABSORB_URL);
		s_img_skillResult_noeffect = UIWindows.createImage(IMG_SKILLRESULT_NOEFFECT_URL);
		s_img_extra_exp = UIWindows.createImage(IMG_EXTRA_EXP_URL);
		s_img_extra_rongyu = UIWindows.createImage(IMG_EXTRA_RONGYU_URL);

		s_img_extra_zhangong = UIWindows.createImage(IMG_EXTRA_ZHANGONG_URL);
		s_img_extra_jinghun = UIWindows.createImage(IMG_EXTRA_JINGHUN_URL);
		s_img_extra_xiuwei = UIWindows.createImage(IMG_EXTRA_XIUWEI_URL);
	}

	private boolean m_showing;
	private Scene m_scene;

	public void show(Scene scene, int x, int y)
	{
		m_showing = true;
		m_duration = SHOW_DURATION;
		m_x = x + Tools.random(-20, 20);
		m_y = y + Tools.random(-20, 20);;
		m_scene = scene;
		m_img_num = null;

		//根据目标以及数值类型来确定有什么颜色的数字
		GameActor actor = m_scene.getGameActorByID(m_tgtType, m_tgtId);
		if (actor != null) {
			if (actor == m_scene.m_controlledActor) {
				if (m_tgtValue > 0) {
					m_img_num = s_damageNum_green;
				}
				else if (m_tgtValue < 0) {
					m_img_num = s_damageNum_red;
				}
			}
			else {
				if (m_tgtValue > 0) {
					m_img_num = s_damageNum_green;
				}
				else if (m_tgtValue < 0) {
					m_img_num = s_damageNum_yellow;
				}
			}
		}

		m_scene.m_skillResultShow_list.add(this);
	}

	public void show(Scene scene, int x, int y, int extra)
	{
		m_showing = true;
		m_duration = SHOW_DURATION;
		m_x = x;
		m_y = y;
		m_scene = scene;
		m_img_num = null;

		//根据目标以及数值类型来确定有什么颜色的数字
		GameActor actor = m_scene.getGameActorByID(m_tgtType, m_tgtId);
		if (actor != null) {
			if (actor == m_scene.m_controlledActor) {
				if (m_tgtValue > 0) {
					m_img_num = s_damageNum_green;
				}
				else if (m_tgtValue < 0) {
					m_img_num = s_damageNum_red;
				}
			}
			else {
				if (m_tgtValue > 0) {
					m_img_num = s_damageNum_green;
				}
				else if (m_tgtValue < 0) {
					m_img_num = s_damageNum_yellow;
				}
			}
		}

		if (extra == 经验) {
			m_img_extra = s_img_extra_exp;
		}
		else if (extra == 荣誉) {
			m_img_extra = s_img_extra_rongyu;

		}
		else if (extra == 战功) {
			m_img_extra = s_img_extra_zhangong;

		}
		else if (extra == 精魂) {
			m_img_extra = s_img_extra_jinghun;

		}
		else if (extra == 修为) {
			m_img_extra = s_img_extra_xiuwei;

		}
		else {
			m_img_extra = null;
		}

		m_scene.m_skillResultShow_list.add(this);
	}

	public boolean draw(DWGraphics g)
	{
		try {
			if (!m_showing)
				return false;
			if (m_duration > 0) {
				//Tools.debugPrintln(m_tgtResult+":"+穿透+":"+(m_tgtResult & 穿透));
				if ((m_tgtResult & 穿透) != 0) {
					if (m_img_num != null) {
						int Num_w = Tools.getNumberWidth(m_tgtValue)*IMG_DAMAGE_NUM_GREEN_SIZE[0];
						g.drawRegion(
								s_img_skillResult_noeffect,
								0,
								0,
								s_img_skillResult_noeffect.getWidth(),
								s_img_skillResult_noeffect.getHeight(),
								0,
								m_x- m_scene.m_cameraX + (Num_w-s_img_skillResult_noeffect.getWidth()>>1) ,
								m_y- m_scene.m_cameraY - s_img_skillResult_noeffect.getHeight()-3,
								0);

						Tools.drawNumber(g, m_img_num, m_x
								- m_scene.m_cameraX, m_y
								- m_scene.m_cameraY,
								IMG_DAMAGE_NUM_GREEN_SIZE[0],
								IMG_DAMAGE_NUM_GREEN_SIZE[1], m_tgtValue,
								1, 0,
								true);
						m_y -= 2;
					}
				}
				else if ((m_tgtResult & 吸收) != 0) {
					g.drawRegion(s_img_skillResult_absorb, 0, 0, s_img_skillResult_miss.getWidth(),
							s_img_skillResult_miss.getHeight(), 0, m_x - m_scene.m_cameraX, m_y - m_scene.m_cameraY,
							DWGraphics.BOTTOM | DWGraphics.HCENTER);
				}
				else if ((m_tgtResult & 闪避) != 0) {
					g.drawRegion(s_img_skillResult_miss, 0, 0, s_img_skillResult_miss.getWidth(),
							s_img_skillResult_miss.getHeight(), 0, m_x - m_scene.m_cameraX, m_y - m_scene.m_cameraY,
							DWGraphics.BOTTOM | DWGraphics.HCENTER);
				}
				else if ((m_tgtResult & 未命中) != 0) {
					g.drawRegion(s_img_skillResult_miss, 0, 0, s_img_skillResult_miss.getWidth(),
							s_img_skillResult_miss.getHeight(), 0, m_x - m_scene.m_cameraX, m_y - m_scene.m_cameraY,
							DWGraphics.BOTTOM | DWGraphics.HCENTER);
				}
				else {
					if (m_img_extra != null) {
						Tools.drawNumber(g, m_img_num, m_x - m_scene.m_cameraX, m_y - m_scene.m_cameraY,
								IMG_DAMAGE_NUM_GREEN_SIZE[0], IMG_DAMAGE_NUM_GREEN_SIZE[1],
								m_tgtValue, 1, DWGraphics.BOTTOM | DWGraphics.HCENTER, m_img_extra);
					}
					else if (m_img_num != null) {
						if ((m_tgtResult & 暴击) != 0 && m_duration < NUM_SCALE.length) {
							Tools.drawNumber(g, m_img_num, m_x - m_scene.m_cameraX, m_y - m_scene.m_cameraY + 50,
									IMG_DAMAGE_NUM_GREEN_SIZE[0], IMG_DAMAGE_NUM_GREEN_SIZE[1], NUM_SCALE[m_duration],
									NUM_SCALE[m_duration],
									m_tgtValue, 1, DWGraphics.BOTTOM | DWGraphics.HCENTER, true);
						}
						else {
							Tools.drawNumber(g, m_img_num, m_x - m_scene.m_cameraX, m_y - m_scene.m_cameraY,
									IMG_DAMAGE_NUM_GREEN_SIZE[0], IMG_DAMAGE_NUM_GREEN_SIZE[1],
									m_tgtValue, 1, DWGraphics.BOTTOM | DWGraphics.HCENTER, true);
						}
					}
					m_y -= 2;
				}
				m_duration--;
				return true;
			}
			else {
				m_showing = false;
				return false;
			}
		}
		catch (Exception e) {
			Tools.debug(e);
			return false;
		}
	}

	public static final int 未命中 = 1 << 0;
	public static final int 暴击 = 1 << 1;
	public static final int 穿透 = 1 << 2;
	public static final int 吸收 = 1 << 3;
	public static final int 闪避 = 1 << 4;

	public static final int 经验 = 1;
	public static final int 荣誉 = 2;
	public static final int 战功 = 3;
	public static final int 精魂 = 4;
	public static final int 修为 = 5;

	public static final int SHOW_DURATION = 8;

	public static final float[] NUM_SCALE = { 1.0f, 1.0f, 1.2f, 1.4f, 1.6f, 1.8f, 2.0f, 2.2f, 2.2f };
}
